// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
// ts游戏主界面
const {ccclass, property} = cc._decorator;
/* // const {compare} =compare */
import compare from "./compare";

@ccclass
export default class mainGame extends cc.Component {

    @property
    randomX: number = 0; 

    @property
    randomY: number = 0;
    
    @property(cc.Node)
    PurpleMonster: cc.Node = null;//monster

    @property(cc.Node)
    floor:cc.Node = null;

    // @property
    // // monsterHeight: number = 0;
    // monsterAdmin:any;

    // @property(cc.Label)
    // score:cc.Label = null;
    // @property(cc.Label)
    // default: 0;
    // @property(cc.Label)
    // displayName:'Score(Monster)';//属性名
    // @property(cc.Label)
    // tooltip:'这里显示摘星星的分数';//鼠标移动到该score时显示tooltip内容
    
    //properties
    // {
    //     default : 0;
    //     displayName: "Score (player)",
    //     tooltip: "The score of player",
    // }

    @property(cc.Prefab)
    starPreFab = null;

    public static connos:any;        

    onLoad () {
        /* // this.randomY = this.floor.y + this.floor.height / 2;
        // cc.log("compare__",compare);
        // var node = this.node;
        // cc.log("mainGame.node___",node)//mainGame.nam =  Canvas; */
    //    mainGame.randomStarFun()
    }

    public randomStarFun() {
        //调用星星的预制资源
        var createStar = cc.instantiate(this.starPreFab);
        this.node.addChild(createStar);

        createStar.setPosition(this.randomPosition());

        //挂载star的预制场景到初始项目预览主场景;
        createStar.getComponent('star').gameScene = this;/* //compare = game;gameScene = game */

        // /* console.log("createStar__",createStar);//createStar.name == star */

    }
    
    private randomPosition() {
        //this.PurpleMonster怪兽节点挂载组件(script)compare jumpHeight 150
        var starY = this.floor.y + Math.random() * this.PurpleMonster.getComponent("compare").jumpHeight + 50;
        var starX = 0;
        var index = this.node.width / 2;//在正中间;

        starX = (Math.random() - 0.5 ) * 2 * index;
        /* // cc.log("this.floor.y【 ",this.floor.y,'】'); */
        return cc.v2(starX,starY);
    }

    private andMonsterInstance() {//gameScene  compare mainGame
        //获得距离
        cc.log("mainGame__andMonsterInstance");
         var monsterPosition = this.getComponent('PurpleMonster').getPosition();
        cc.log("monsterPosition【",monsterPosition.getPosition(),'】');
        //mag 返回该向量的长度
        var calcLength = this.node.position.sub(monsterPosition).mag();
        return calcLength;
    }

    private createStar() {
        /* 这里不执行 代码不生效 */
        this.randomStarFun();
        
        this.node.destroy();

    }

    private gameOver() {
        /* 这里不执行 代码不生效 */
        this.PurpleMonster.stopAllActions(); /* //停止 player 节点的跳跃动作 */
        cc.director.loadScene('gameScene');
        /* //cc.log("gameOver___",this.gameOver);*/
    }
 

    start () {
        //parent 物体 通过parent获得场景的所有子节点; 作用:manager
         mainGame.connos = [];
        mainGame.connos = this.node.children;
        console.log("this.connos___",mainGame.connos);//这里加载Canvas主场景下的所有子节点;
        /*// let a = this.PurpleMonster.getPosition();
        // cc.log("a__",a); */
        
    }

    // update (dt) {}
}
